//
//  MainViewController.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/5.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
      addChildViewControllers()
         setupComposedButton()
    }
    ///懒加载按钮
    private lazy var composedButton: UIButton = {
        return UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
    }()
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
       tabBar.bringSubviewToFront(composedButton)
    }
    ///添加按钮frame
    private func setupComposedButton(){
        tabBar.addSubview(composedButton)
      let  count: Int = childViewControllers.count
        let w = tabBar.bounds.width/CGFloat(count)-1
        composedButton.frame = CGRectInset(tabBar.bounds, 2*w, 0)
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
    }
    @objc private func clickComposedButton(){
        let vm : UIViewController
        if UserAccountVM.shareUserAccount.userLogon{
            vm = ComposedViewController()
        }else{
            vm = OAuthViewController()
        }
        let nav = UINavigationController(rootViewController: vm)
        presentViewController(nav, animated: true, completion: nil)
    }
    ///添加所有子控制器
    private func addChildViewControllers() {
        tabBar.tintColor=UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(MessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(ProfileTableViewController(), imageName: "tabbar_profile", title: "我")
    }

    private func addChildViewController(vc: UIViewController, imageName: String, title: String) {
        vc.title=title
        vc.tabBarItem.image=UIImage(named: imageName)
        let na = UINavigationController(rootViewController: vc)
        addChildViewController(na)
    }
}
